package io.zcoder.innoway.activities;


import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Optional;
import io.zcoder.innoway.R;
import io.zcoder.innoway.widgets.TextViewCustom;

public class BaseActivity extends AppCompatActivity {

    @Nullable
    @BindView(R.id.common_back)
    ImageView mBack;
    @Nullable
    @BindView(R.id.common_title)
    TextViewCustom mTitle;
    private boolean isDetachedFromWindow = false;

    @Optional
    @OnClick(R.id.common_back)
    void onBack() {
        onBackPressed();
    }

    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCanceledOnTouchOutside(false);
    }

    public void showAlertPopup(String title, String message, String positiveBtn,
                               DialogInterface.OnClickListener listener, String negativeBtn) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle(title);
        alertDialogBuilder.setMessage(message);
        alertDialogBuilder.setPositiveButton(positiveBtn, listener);
        alertDialogBuilder.setNegativeButton(negativeBtn, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialogBuilder.show();
    }

    public void showAlert(String title, String message) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle(title);
        alertDialogBuilder.setMessage(message);
        alertDialogBuilder.setPositiveButton(getString(R.string.ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialogBuilder.show();
    }

    public void showBack(boolean show) {
        if (show) {
            mBack.setVisibility(View.VISIBLE);
        } else {
            mBack.setVisibility(View.GONE);
        }
    }

    public void showProgress() {

        mProgressDialog.setMessage(getString(R.string.loading));

        if (!mProgressDialog.isShowing() && !isFinishing()) {
            mProgressDialog.show();
        }
    }

    public synchronized void hideProgress() {
        if (mProgressDialog != null && mProgressDialog.isShowing() && !isDetachedFromWindow) {
            mProgressDialog.dismiss();
        }
    }

    public void setTitle(String title) {
        showTitle(true);
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }

    public void showTitle(boolean show) {
        if (mTitle != null) {
            mTitle.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void onDetachedFromWindow() {
        isDetachedFromWindow = true;
        super.onDetachedFromWindow();
    }
}
